package process;

import ui.ProcUi;

/*
 * 进程管理
 */

public class Index {
	private static int time = 0;
	private static boolean debug = true;
	static {
		int[] devNums = { 0, 0, 0 };
		boolean result = (Boolean) controller.Ram.getObject()
				.createProc(1, -1, -1, devNums).get("result");
		System.out.println("not busy尝试申请内存：" + result);

		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				storage.Pcb[] pcbs = controller.Ram.getObject().getPcbs();

				boolean sign = false;

				while (true) {
					try {

						if (debug) {
							System.out.println("当前时间片：" + time);
						}

						for (int i = 0; i < pcbs.length; i++) {

							if (pcbs[i].getStatus() != null) {

								if (pcbs[i].getStatus().equals("运行态")) {
									sign = true;
									pcbs[i].setRunTime(pcbs[i].getRunTime() + 1);
									ProcUi.getLogcatStrings().add("进程"
											+ pcbs[i].getPid() + "处于运行态"
											+"\n"+"进程"
											+ pcbs[i].getPid() + "运行了: "
											+ pcbs[i].getRunTime()+"s");
									
									if (ProcUi.getLogcatStrings().size()>15) {
										ProcUi.getLogcatStrings().remove(0);
										ProcUi.getLogcatStrings().remove(0);
									}
									
									
									
									if (debug) {
										System.out.println("进程"
												+ pcbs[i].getPid() + "运行了: "
												+ pcbs[i].getRunTime()+"s");
										System.out.println("进程"
												+ pcbs[i].getPid()
												+ "总运行所需的时间: "
												+ pcbs[i].getNeedTime());
										System.out.println("进程"
												+ pcbs[i].getPid() + "处于运行态");
									}

									if (pcbs[i].getRunTime() == pcbs[i]
											.getNeedTime()) {
										// pcbs[i].setStatus("结束态");
										controller.Ram.getObject().destroyProc(
												pcbs[i].getPid());
										ProcUi.getLogcatStrings().add("进程"
												+ pcbs[i].getPid()
												+ ":进入结束态");
										if (ProcUi.getLogcatStrings().size()>15) {
											ProcUi.getLogcatStrings().remove(0);
										}
										if (debug)
											System.out.println("进程"
													+ pcbs[i].getPid()
													+ ":进入结束态");

										changeWaitOrReadyToRun(pcbs, i, "就绪态");
									}

									else if (time >= 5) {
										pcbs[i].setStatus("就绪态");
										
										ProcUi.getLogcatStrings().add("进程"
												+ pcbs[i].getPid()
												+ ":进入就绪态,时间片out");
										if (ProcUi.getLogcatStrings().size()>15) {
											ProcUi.getLogcatStrings().remove(0);
										}
										if (debug)
											System.out.println("进程"
													+ pcbs[i].getPid()
													+ ":进入就绪态,时间片out");
										time = 0;
										changeWaitOrReadyToRun(pcbs, i, "就绪态");
									}

									break;
								}

							} else {
								// System.out.println("pcbs["+i+"]=null");
							}
						}

						// 进入闲逛进程
						if (!sign) {
							changeWaitOrReadyToRun(pcbs, 0, "就绪态");
							// System.out.println("闲逛中。。。");
						} else {
							sign = false;
						}

						time++;
						Thread.sleep(1000);

					} catch (InterruptedException e) {
						if (debug)
							System.out.println("线程异常！");
					}
				}

			}
		});
		thread.setDaemon(true);
		thread.start();
	}

	private static void changeWaitOrReadyToRun(storage.Pcb[] pcbs, int index,
			String type) {
		// time=0;

		storage.Pcb tempPcb = pcbs[0];

		for (int i = index + 1; i < pcbs.length; i++) {
			if (pcbs[i].getStatus() != null) {
				if (pcbs[i].getStatus().equals(type)) {

					if (tempPcb.getLevel() < pcbs[i].getLevel()) {

						tempPcb = pcbs[i];
					}

				}
			}
		}

		for (int i = 0; i <= index; i++) {
			if (pcbs[i].getStatus() != null) {
				if (pcbs[i].getStatus().equals(type)) {
					if (tempPcb.getLevel() < pcbs[i].getLevel()) {

						tempPcb = pcbs[i];
					}

				}
			}
		}

		if (tempPcb.getPid() == 1) {
			// 闲逛
			if (type.equals("就绪态")) {
				changeWaitOrReadyToRun(pcbs, index, "阻塞态");
			} else {
				if (time >= 5) {
					time = 0;
				}
				tempPcb.setStatus("运行态");
				ProcUi.getLogcatStrings().add("闲逛进程正在运行");
				ProcUi.getLogcatStrings().add("闲逛中。。。");
				if (ProcUi.getLogcatStrings().size()>15) {
					ProcUi.getLogcatStrings().remove(0);
					ProcUi.getLogcatStrings().remove(0);
				}
				if (debug) {
					System.out.println("闲逛中。。。");
				}
			}

		} else {
			// 尝试唤醒就绪态或阻塞
			if (!tryChangeReadyToRun(tempPcb)) {
				changeWaitOrReadyToRun(pcbs, index + 1, type);
			}
		}

	}

	private static boolean tryChangeReadyToRun(storage.Pcb tempPcb) {
		if (tempPcb.isHaveDev()) {
			tempPcb.setStatus("运行态");
		} else {

			if (controller.Dev.getObject().apply(tempPcb.getPid(),
					tempPcb.getDevANumber(), tempPcb.getDevBNumber(),
					tempPcb.getDevCNumber())) {
				tempPcb.setStatus("运行态");

			} else {
				tempPcb.setStatus("阻塞态");
				ProcUi.getLogcatStrings().add("进程"
						+ tempPcb.getPid()
						+ "处于阻塞态");
				if (ProcUi.getLogcatStrings().size()>20) {
					ProcUi.getLogcatStrings().remove(0);
				}
				if (debug) {
					System.out.printf("进程%d处于阻塞态\n", tempPcb.getPid());
				}
				return false;
			}

		}

		return true;

	}

}